from django.http import Http404
from django.shortcuts import render
from myblog.models import Article


# Create your views here.


def get_archives(request):
    article_date_list = []
    try:
        date_list = Article.objects.all().dates('pub_date', 'month', order='DESC')
        for date in date_list:
            article_list = Article.objects.filter(pub_date__year=date.year, pub_date__month=date.month).order_by(
                '-pub_date')
            article_date_list.append((date, article_list))

    except Exception as e:
        raise Http404

    return render(request, 'blog/archives.html', {"article_date_list": article_date_list})
